package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


public class ContactHelper extends HelperWithWebDriverBase{

	
	private int lastContactId;


	public ContactHelper(ApplicationManager manager){
	super(manager);
	lastContactId = Integer.parseInt(
			manager.getHibernateHelper().getLastContactId());
	}
	//private Contacts contactCache;
	private Contacts contactModel;
	
	public Contacts getContacts() {
	if(contactModel== null){
		contactModel = manager.getHibernateHelper().getContacts();
		}
		return contactModel;
}
	
	public Contacts getUiContacts() {
		HashSet<ContactObject> set= new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
			WebElement form = findElements(By.tagName("tbody")).get(0);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			Pattern p = Pattern.compile(".*\\((.*)\\)");
			 Matcher m = p.matcher(title);
			if (m.matches()){
			title = m.group(1);
			}; 
//			title = title.substring("Select (".length(),title.length()-")".length());
			ContactObject contact = new ContactObject()
			.setTitle(title)
			.setId(checkbox.getAttribute("value"));
			set.add(contact);
		}
		return new Contacts(set);
	}

	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().goToContactCreationList();
			fillContactCreationForm(contactObject, true);
		submitContactCreation();
		manager.getNavigationHelper().goToHomePage();
		lastContactId = lastContactId+1;
		contactObject.setId("" +lastContactId ); 
		contactModel = contactModel.withAdded(contactObject);
	}
	
	
	public void modifyContact(ContactObject someContact, ContactObject contactObject) {
		initChangingContact(someContact);
		fillContactCreationForm(contactObject, false);
		submitContactModification();
		manager.getNavigationHelper().goToHomePage();
		lastContactId = lastContactId+1;
		contactObject.setId("" +lastContactId ); 
		contactModel = contactModel.withoutRemoved(contactObject).withAdded(contactObject);
	}



	public void deleteContact(ContactObject someContact) {
		initChangingContact(someContact);
		submitContactDeletion();
		manager.getNavigationHelper().goToHomePage();
		contactModel = contactModel.withoutRemoved(someContact);
	}
	
	public void fillContactCreationForm(ContactObject contactObject, boolean fillgroup) {
	
	type(By.name("firstname"), contactObject.firstname);
	type(By.name("lastname"), contactObject.lastname);
	type(By.name("address"), contactObject.address);
	type(By.name("home"), contactObject.home);
	type(By.name("mobile"), contactObject.mobile);
	type(By.name("work"), contactObject.work);
	type(By.name("email"), contactObject.email);
	type(By.name("email2"), contactObject.email2);
	selectRandom(By.name("bday"));
	selectRandom(By.name("bmonth"));
	type(By.name("byear"), contactObject.year);
	if(fillgroup){
		selectRandom(By.name("new_group"));
	}
	type(By.name("address2"), contactObject.address2);
	type(By.name("phone2"), contactObject.phone2);
					}

		public void submitContactCreation() {
		click(By.name("submit"));
	}
	private void submitContactDeletion() {
		By.xpath("(//input[@name='update'])[2]");
	}
	private void submitContactModification() {
		click(By.name("update"));
	}
	//private void initChangingContact(ContactObject someContact) {
	//	new index = somecontact.id
	//	click(By.xpath("//tr[@name='entry'][" + (index+1) + "]/td[7]/a/img")); 
 //}

private void initChangingContact(ContactObject someContact) {
		click(By.linkText("home"));
		click(By.xpath("(//img[@alt='Edit'])[somecontact.id]"));
	}

				
//		WebElement Contactform = findElements(By.tagName("form")).get(2);
//		List<WebElement> lines = Contactform.findElements(By.tagName("tr"));
//		for (WebElement line : lines) {
//			WebElement checkbox = findElements(By.tagName("td")).get(1);
//			String title = checkbox.getAttribute("title");
//			title = title.substring("Select (".length(),title.length()-")".length());
//			if (title.equals(someContact.title)){
//				findElements(By.tagName("tr")).get(7);	
//				click(By.linkText("Edit"));
	//	};
	}
		
		//		click(By.id("id6"));
	//	click(By.cssSelector("img[alt=\"Edit\"]"));
	//click(By.xpath("//input[@value='"+someContact.id+"']"));
	
//}
//public void fillContactCreationForm1(ContactObject contactObject) {
	
//type(By.name("firstname"), contactObject.firstname);
//type(By.name("lastname"), contactObject.lastName);
//type(By.name("address"), contactObject.address);
//type(By.name("home"), contactObject.home);
//type(By.name("mobile"), contactObject.mobile);
//type(By.name("work"), contactObject.work);
//type(By.name("email"), contactObject.email);
//type(By.name("email2"), contactObject.email2);
//select(By.name("bday"), contactObject.bday);
//select(By.name("bmonth"), contactObject.bmonth);
//type(By.name("byear"), contactObject.byear);

//if ( driver.findElements(By.name("new_group")).size()>0){
//select(By.name("new_group"), contactObject.new_group);	
//}
//type(By.name("address2"), contactObject.address2);
//type(By.name("phone2"), contactObject.phone2);
//			}



//public void fillContactModificationForm(ContactModificationOblect modifyContactData) {
					
//		type(By.name("firstname"), modifyContactData.name);
//	type(By.name("lastname"), modifyContactData.lastname);
//	type(By.name("address"), modifyContactData.address);
//	type(By.name("home"), modifyContactData.home);
//	type(By.name("mobile"), modifyContactData.mobile);
//	type(By.name("work"), modifyContactData.work);
//	type(By.name("email"), modifyContactData.email);
//	type(By.name("email2"), modifyContactData.email2);
//	select(By.name("bday"), modifyContactData.bday);
//	select(By.name("bmonth"), modifyContactData.bmonth);
//	type(By.name("byear"), modifyContactData.byear);
//	type(By.name("address2"), modifyContactData.address2);
//	type(By.name("phone2"), modifyContactData.phone2);
//}